package com.bjtu.boatmq.broker;


import com.bjtu.boatmq.broker.config.BrokerConfig;
import com.bjtu.boatmq.broker.service.BrokerService;
import com.bjtu.boatmq.broker.service.outer.BrokerLogger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class BrokerStartup {
    private BrokerService brokerService = null;
    private Properties properties = null;
    private BrokerConfig brokerConfig = null;

    public boolean initialize(String[] args){
        try {
            properties = createProperties(args);
            brokerConfig = createBrokerConfig(properties);
            brokerService =  createBrokerService(brokerConfig);
        }catch (Exception e){
            e.printStackTrace();
            BrokerLogger.write(e);
            return false;
        }
        return true;
    }
    public Properties createProperties(String[] args) throws IOException{
        Properties properties = new Properties();
        String propertiesFilePath = "server.properties";
        // 根据args获取配置的文件地址
        if(args.length > 0){
            propertiesFilePath = args[0];
        }
        // 1.根据args读取对应的配置文件路径：目录+文件名
        try {
            // 2.打开文件流，开始读取配置文件
            InputStream in = BrokerStartup.class.getClassLoader().getResourceAsStream(propertiesFilePath);
            if(in == null){
                System.out.println("没有指定配置文件sever.properties.");
            }else{
                System.out.println("成功读取配置文件");
                properties.load(in);
            }
        } catch (Exception e) {
            e.printStackTrace();
            BrokerLogger.write("10001", "failed to read properties.");
            return null;
        }
        return properties;
    }

    public BrokerConfig createBrokerConfig(Properties properties) throws Exception{
        try{
            brokerConfig = new BrokerConfig(properties);
        }catch (Exception e){
            BrokerLogger.write("10002", "failed to load configuration.");
            throw e;
        }
        return brokerConfig;
    }

    public BrokerService createBrokerService(BrokerConfig brokerConfig) throws Exception{
        return new BrokerService(brokerConfig);
    }

    public void start() throws Exception {
        brokerService.start();
    }

    public static void main(String[] args) {
        BrokerStartup brokerStartup =  new BrokerStartup();
        try {
            if(brokerStartup.initialize(args)){
                brokerStartup.start();
            }
        }catch (Exception e){
            BrokerLogger.write(e);
            System.out.println("failed to start up broker.");
            BrokerLogger.write("10000", "failed to start up broker.");
        }
    }


    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public BrokerConfig getBrokerConfig() {
        return brokerConfig;
    }

    public void setBrokerConfig(BrokerConfig brokerConfig) {
        this.brokerConfig = brokerConfig;
    }

    public BrokerService getBrokerService() {
        return brokerService;
    }

    public void setBrokerService(BrokerService brokerService) {
        this.brokerService = brokerService;
    }
}
